<template>
  <el-dialog
    :title="!dataForm.commentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户头像" prop="details">
      <el-input v-model="dataForm.details" placeholder="用户头像"></el-input>
    </el-form-item>
    <el-form-item label="" prop="isqanda">
      <el-input v-model="dataForm.isqanda" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="replayId">
      <el-input v-model="dataForm.replayId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="views">
      <el-input v-model="dataForm.views" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="likes">
      <el-input v-model="dataForm.likes" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="answerStatus">
      <el-input v-model="dataForm.answerStatus" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="uid">
      <el-input v-model="dataForm.uid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="id">
      <el-input v-model="dataForm.id" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          commentId: 0,
          details: '',
          isqanda: '',
          createTime: '',
          replayId: '',
          views: '',
          likes: '',
          answerStatus: '',
          uid: '',
          id: ''
        },
        dataRule: {
          details: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          isqanda: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          replayId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          views: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          likes: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          answerStatus: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          uid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          id: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.commentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.commentId) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/qanda/info/${this.dataForm.commentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.details = data.qanda.details
                this.dataForm.isqanda = data.qanda.isqanda
                this.dataForm.createTime = data.qanda.createTime
                this.dataForm.replayId = data.qanda.replayId
                this.dataForm.views = data.qanda.views
                this.dataForm.likes = data.qanda.likes
                this.dataForm.answerStatus = data.qanda.answerStatus
                this.dataForm.uid = data.qanda.uid
                this.dataForm.id = data.qanda.id
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/qanda/${!this.dataForm.commentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'commentId': this.dataForm.commentId || undefined,
                'details': this.dataForm.details,
                'isqanda': this.dataForm.isqanda,
                'createTime': this.dataForm.createTime,
                'replayId': this.dataForm.replayId,
                'views': this.dataForm.views,
                'likes': this.dataForm.likes,
                'answerStatus': this.dataForm.answerStatus,
                'uid': this.dataForm.uid,
                'id': this.dataForm.id
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
